<template>
  <div class="">
    <!-- 条件筛选-----查询 -->
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div>
          <el-form-item>
            <el-input v-model="key" clearable placeholder="昵称、手机、邀请码、标签、姓名、昵称" style="width: 400px;" />
          </el-form-item>
          <el-form-item>
            <label>会员等级：</label>
            <el-select v-model="ulevel" placeholder="请选择" style="width: 100px;">
              <el-option label="全部" :value="-1" />
              <el-option v-for="item in Object.keys(uLevel)" :label="uLevel[item]" :value="Number(item)" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <label>服务商身份：</label>
            <el-select v-model="dls" placeholder="请选择" style="width: 110px;">
              <el-option label="全部" :value="-1" />
              <el-option v-for="item in Object.keys(dlsList)" :label="dlsList[item]" :value="Number(item)" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <label>状态：</label>
            <el-select v-model="state" placeholder="请选择" style="width: 100px;">
              <el-option v-for="item in Object.keys(UserState)" :label="UserState[item]" :value="Number(item)" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item>
            <label>排序：</label>
            <el-select v-model="sortfield" placeholder="请选择" style="width: 140px;">
              <el-option v-for="item in sortOptions" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-date-picker v-model="dateTime" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
              start-placeholder="开始日期" end-placeholder="结束日期" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="init">查询</el-button>
            <el-button type="primary" @click="addUser">新增用户</el-button>
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item>
            <el-button type="primary" :loading="exportLoading" @click="doExport">导出Excel</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <!-- 列表数据 -->
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%" @sort-change="onSortChange">
      <el-table-column prop="userid" label="ID" width="60" align="center" />
      <el-table-column prop="username" label="用户信息" width="180" align="center">
        <template slot-scope="{row}">
          <div>昵称：{{ row.nickname }}</div>
          <div v-if="row.truename">姓名：{{ row.truename }}</div>
          <div v-if="row.phone">手机号：{{ row.phone }}</div>
          <div v-if="row.emaill">邮箱：{{ row.emaill }}</div>
          <div v-if="row.invcode">邀请码：{{ row.invcode }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="bankname" label="收款信息" align="center">
        <template slot-scope="{row}">
          <div>银行名称：{{ row.bankname }}</div>
          <div>银行卡号：{{ row.bankcard }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="balance" label="余额" sortable="custom" align="center" />
      <el-table-column prop="amtB2" label="渠道津贴" sortable="custom" width="102" align="center" />
      <el-table-column prop="amtB1" label="业务津贴" sortable="custom" width="102" align="center" />
      <el-table-column prop="amtB3" label="管理津贴" sortable="custom" width="102" align="center" />
      <el-table-column prop="amtO1" label="调解服务" sortable="custom" width="102" align="center" />
      <el-table-column prop="amtO2" label="签约服务商" sortable="custom" width="102" align="center" />
      <el-table-column prop="amtO3" label="结清服务" sortable="custom" width="102" align="center" />
      <el-table-column prop="tag" label="标签" align="center" />
      <el-table-column prop="uLevel" label="会员等级" align="center" width="110">
        <template slot-scope="{row}">
          <el-select v-model="row.uLevel" @change="(val) => { return SetuLevel(val, row) }">
            <el-option v-for="item in Object.keys(uLevel)" :label="uLevel[item]" :value="Number(item)" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="dls" label="服务商身份" align="center" width="110">
        <template slot-scope="{row}">
          <el-select v-model="row.dls" @change="(val) => { return SetDls(val, row) }">
            <el-option v-for="item in Object.keys(dlsList)" :label="dlsList[item]" :value="Number(item)" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center" width="110">
        <template slot-scope="{row}">
          <el-select v-model="row.state" @change="(val) => { return SetUsersState(val, row) }">
            <el-option v-if='item != 0' v-for="item in Object.keys(UserState)" :label="UserState[item]" :value="item" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="注册时间" align="center">

        <template slot-scope="{row}">
          <div>{{ row.createtime.replace("T", " ") }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="lastloginip" label="登录IP" align="center" /> -->
      <el-table-column label="操作" width="200" align="center">

        <template slot-scope="{row}">
          <el-button size="mini" type="primary"
            @click="$router.push({ name: 'balance', query: { userid: row.userid } })">余额</el-button>
          <el-button size="mini" type="primary"
            @click="$router.push({ name: 'BalanceRecord', query: { userid: row.userid } })">资金记录
          </el-button>
          <!-- <el-button size="mini" type="primary" style="margin-top: 10px;" @click="openRecharge(row)">充值
          </el-button> -->
          <!-- <el-button size="mini" type="warning" style="margin-top: 10px;"
            @click="$router.push({ name: 'Team', query: { useraddress: row.userName } })">服务商关系
          </el-button>
          <el-button size="mini" type="primary" style="margin-top: 10px;"
            @click="$router.push({ name: 'withdrawal', query: { userid: row.userid } })">提现记录
          </el-button> -->
          <!-- <el-button size="mini" type="primary" style="margin-top: 10px;" @click="openAddTag(row)">修改用户标签
          </el-button>
          <el-button size="mini" type="primary" style="margin-top: 10px;" @click="openSetUserInfo(row)">修改用户信息
          </el-button> -->

          <el-dropdown trigger="click">
            <el-button style="margin-top: 10px;margin-left: 10px;" size="mini" type="success">更多<i
                class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="$router.push({ name: 'withdrawal', query: { userid: row.userid } })">提现记录
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="warning" style="margin-top: 10px;"
                  @click="$router.push({ name: 'Team', query: { useraddress: row.username } })">服务商关系
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="openRecharge(row)">充值
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="openAddTag(row)">修改用户标签
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="openSetUserInfo(row)">修改用户信息
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="openSetBankinfo(row)">修改收款信息
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item><el-button size="mini" type="primary" style="margin-top: 10px;"
                  @click="openSetUserInv(row)">修改推荐人
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-form :inline="true" style="padding-top: 10px;">
      <el-form-item v-if="total != null">
        <div class="flex">
          <div style="font-size: 16px;font-weight: bold;">津贴汇总:</div>
          <div style="margin-left: 20px;">【 业务津贴：{{ total.amtB1 }} 】</div>
          <div style="margin-left: 20px;">【 渠道津贴：{{ total.amtB2 }} 】</div>
          <div style="margin-left: 20px;">【 管理津贴：{{ total.amtB3 }} 】</div>
        </div>
        <div class="flex">
          <div style="font-size: 16px;font-weight: bold;">服务汇总:</div>
          <div style="margin-left: 20px;">【 调解服务：{{ total.amtO1 }} 】</div>
          <div style="margin-left: 20px;">【 签约服务商：{{ total.amtO2 }} 】</div>
          <div style="margin-left: 20px;">【 结清服务：{{ total.amtO3 }} 】</div>
        </div>
        <div class="flex">
          <div style="font-size: 16px;font-weight: bold;">总服务额:</div>
          <div style="margin-left: 20px;">【 {{ total.amtSelf }} 】</div>
        </div>
      </el-form-item>
    </el-form>
    <!-- 分页 -->
    <div class="block " style="padding-bottom: 10px;">
      <el-pagination background :current-page="PageIndex" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="totalCount" @size-change="idx => { pageSize = idx }"
        @current-change="idx => { PageIndex = idx }" />
    </div>

    <!-- 系统充值 -->
    <digRechargeVue v-if="modalStatus == 1" :info="currentInfo" />

    <!-- 修改用户标签 -->
    <digaddtag v-if="modalStatus == 2" :info="currentInfo" />

    <!-- 新增用户 -->
    <digaddUser v-if="modalStatus == 3" :info="currentInfo" />

    <!-- 修改用户信息 -->
    <digsetuserinfo v-if="modalStatus == 4" :info="currentInfo" />

    <!-- 修改用户收款信息 -->
    <digsetbankinfo v-if="modalStatus == 5" :info="currentInfo" />

    <!-- 修改用户推荐人 -->
    <digSetUserInv v-if="modalStatus == 6" :info="currentInfo" />

  </div>
</template>

<script>
import { userlist, setuserstate, setDls, SetStoreManager, setULevel, setUserisjd, SetCloseState, userlistExcel } from '@/api/users.js'
import digRechargeVue from '../components/digRechargeSys.vue'
import digaddtag from './components/digAddTag.vue'
import digaddUser from './components/digAddUser.vue'
import digsetuserinfo from './components/digSetUserInfo.vue'
import digsetbankinfo from './components/digAddBankInfo.vue'
import digSetUserInv from './components/digSetUserInv.vue'
import { GetEnum, coinlist } from "@/api/ctrls.js";
export default {
  name: 'Name',
  components: { digRechargeVue, digaddtag, digaddUser, digsetuserinfo, digsetbankinfo,digSetUserInv },
  data() {
    return {
      // 筛选参数
      key: '',
      state: 0,
      dls: -1,
      ulevel: -1,
      dateTime: "", // 时间查询
      sortfield: "",

      // 列表相关
      listLoading: false,
      tableData: [],
      totalCount: 0,
      PageIndex: 1,
      pageSize: 10,
      total: {},

      // 排序
      sortOptions: [
        {
          value: 'balance_1',
          label: '余额顺序'
        },
        {
          value: 'balance_2',
          label: '余额倒序'
        },
        {
          value: 'amtB1_1',
          label: '业务津贴顺序'
        },
        {
          value: 'amtB1_2',
          label: '业务津贴倒序'
        },
        {
          value: 'amtB2_1',
          label: '渠道津贴顺序'
        },
        {
          value: 'amtB2_2',
          label: '渠道津贴倒序'
        },
        {
          value: 'amtB3_1',
          label: '管理津贴顺序'
        },
        {
          value: 'amtB3_2',
          label: '管理津贴倒序'
        },
        {
          value: 'amtO1_1',
          label: '调解服务顺序'
        },
        {
          value: 'amtO1_2',
          label: '调解服务倒序'
        },
        {
          value: 'amtO2_1',
          label: '签约服务商顺序'
        },
        {
          value: 'amtO2_2',
          label: '签约服务商倒序'
        },
        {
          value: 'amtO3_1',
          label: '结清服务顺序'
        },
        {
          value: 'amtO3_2',
          label: '结清服务倒序'
        },
      ],

      UserLevelInfo: {}, // 提现状态实体
      UserState: {}, // 用户状态
      uLevel: {}, // 用户等级
      dlsList: {}, // 服务商等级

      coinList: [], // 币种列表

      modalStatus: 0, // 打开组件弹窗参数
      currentInfo: {}, // 当前用户信息

      exportLoading: false, // 导出loading
    }
  },
  watch: {
    PageIndex(val) {
      this.getData()
    },
    pageSize(val) {
      this.getData()
    },
    modalStatus(val) {
      this.getData()
    }
  },
  async mounted() {
    this.getData()
    this.GetEnum();
    this.GetCoin();
  },
  methods: {
    /** 刷新加载 */
    init() {
      this.PageIndex = 1
      this.getData()
    },
    /**打开系统充值弹窗 */
    openRecharge(info) {
      this.currentInfo = info;
      this.modalStatus = 1;
    },
    /**打开修改用户标签弹窗 */
    openAddTag(info) {
      this.currentInfo = info;
      this.modalStatus = 2;
    },
    /**打开修改用户标签弹窗 */
    addUser(info = {}) {
      this.currentInfo = info;
      this.modalStatus = 3;
    },
    /**打开修改用户信息弹窗 */
    openSetUserInfo(info) {
      this.currentInfo = info;
      this.modalStatus = 4;
    },
    /**打开修改用户收款信息弹窗 */
    openSetBankinfo(info) {
      this.currentInfo = info;
      this.modalStatus = 5;
    },
    /**打开修改用户推荐人弹窗 */
    openSetUserInv(info) {
      this.currentInfo = info;
      this.modalStatus = 6;
    },
    // 排序监听
    onSortChange(column) {
      console.log('aas', column)
      if (column.order == 'ascending') {
        this.sortfield = column.prop + '_1';
      } else if (column.order == 'descending') {
        this.sortfield = column.prop + '_2';
      } else {
        this.sortfield = '';
      }
      this.init();
    },
    /////////////////////// 导出报表  ///////////////////////
    // 导出报表
    async doExport() {
      this.exportLoading = true;
      const res = await userlistExcel({
        key: this.key,
        state: this.state,
        dls: this.dls,
        ulevel: this.ulevel,
        sortfield: this.sortfield,
        stime: this.dateTime ? this.dateTime[0] : "",
        etime: this.dateTime ? this.dateTime[1] : "",
      });
      this.exportLoading = false;
      if (res) {
        console.log('导出网络数据：：', res);
        // 文件名
        const fileName = "用户列表.xlsx";
        // 处理导出数据并下载
        this.handleExcel(res, fileName);
      }
    },
    /**转换数据类型  下载Excel报表 */
    handleExcel(data, fileName) {
      //转换数据类型
      const xlsx = "application/vnd.ms-excel;charset=utf-8";
      const blob = new Blob([data], { type: xlsx });
      console.log(blob);

      // 转换完成，创建一个a标签用于下载
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = URL.createObjectURL(blob);
      a.setAttribute("download", fileName);
      document.body.appendChild(a);
      a.click();
      a.remove();
    },
    // ////////////////////////////////////////// 数据请求 /////////////////////////////////////////////
    /**修改是否节点 */
    async SetIsjd(val, id) {
      const { success, msg } = await setUserisjd({
        userid: id,
        isjd: val
      });
      this.getData();
      this.$message({
        message: msg,
        type: success ? "success" : "error"
      });
    },
    /** 修改等级 */
    async SetuLevel(val, item, type) {
      const { success, msg } = await setULevel({
        uid: item.userid,
        level: val
      })
      this.getData()
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    /** 修改服务商等级 */
    async SetDls(val, item) {
      const { success, msg } = await setDls({
        uid: item.userid,
        level: val
      })
      this.getData()
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    /** 修改用户角色 */
    async SetStore(val, item) {
      const { success, msg } = await SetStoreManager({
        uid: item.userid,
        state: val
      })
      this.getData()
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    /**修改状态 */
    async SetUsersState(val, item, type) {
      const { success, msg } = await setuserstate({
        uid: item.userid,
        state: val
      })
      this.getData()
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    /** 设置提现、转账开关 */
    async SetCloseState(val, item, type) {
      const { success, msg } = await SetCloseState({
        userid: item.userid,
        state: val,
        type: type
      })
      if (success) {
        this.getData()
      }
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    /** 主列表 */
    async getData() {
      this.listLoading = true
      const { list, pageIndex, totalCount, _total } = await userlist({
        key: this.key,
        state: this.state,
        dls: this.dls,
        ulevel: this.ulevel,
        sortfield: this.sortfield,
        stime: this.dateTime ? this.dateTime[0] : "",
        etime: this.dateTime ? this.dateTime[1] : "",
        PageIndex: this.PageIndex,
        PageSize: this.pageSize
      })
      this.listLoading = false
      this.total = _total;
      if (list) {
        this.totalCount = totalCount
        this.tableData = list
      }
    },
    // 获取枚举
    async GetEnum() {
      const res = await GetEnum();
      this.UserLevelInfo = res.data['UserLevel'];
      this.UserState = res.data['UserState'];
      this.mLevel = res.data['mLevel'];
      this.dlsList = res.data['Dls'];
      this.uLevel = res.data['uLevel'];
    },
    // 获取币种
    async GetCoin() {
      const res = await coinlist();
      this.coinList = res;
    },
  }
}
</script>

<style lang="scss">
.el-table .warning-row {
  background: oldlace;
}
</style>
